#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 打开文件
FILE *file_open(const char *path, const char *mode);
// 关闭文件
int file_close(FILE **fp);

// 声明结构体
typedef struct Elenemt_data
{
    char name[128]; // 名字
    char sex[64];   // 性别
    int age;        // 年龄
    int id;         // 学号
    int sco;        // 成绩
} Elenemt_data;

Elenemt_data hqyj_24071[50] =
    {
        {.id = 1, .age = 18, .sco = 100, .sex = "男", .name = "唐三"},
        {.id = 2, .age = 18, .sco = 99, .sex = "男", .name = "萧炎"},
        {.id = 3, .age = 18, .sco = 99, .sex = "男", .name = "叶晨"},
        {.id = 4, .age = 18, .sco = 99, .sex = "女", .name = "小舞"},
        {.id = 5, .age = 18, .sco = 99, .sex = "女", .name = "萧熏儿"},
        {.id = 6, .age = 18, .sco = 99, .sex = "女", .name = "柳如烟"},
};

int main(int argc, char const *argv[])
{
    // ======================= 打开文件 =======================
    FILE *fp = file_open("hello.txt", "r+");


    // 数据
    
    fprintf(fp , "名字:%s\t性别:%s\t学号:%d\t年龄:%d\t成绩:%d\t\n" , 
            hqyj_24071[0].name, hqyj_24071[0].sex, hqyj_24071[0].id,
            hqyj_24071[0].age,  hqyj_24071[0].sco);

    // ======================= 关闭文件 =======================
    file_close(&fp);
    // ======================= 打开文件 =======================
    fp = file_open("hqyj.txt", "r+");

    fscanf(fp , "名字:%s\t性别:%s\t学号:%d\t年龄:%d\t成绩:%d\t\n" ,
            hqyj_24071[7].name, hqyj_24071[7].sex, &hqyj_24071[7].id,
            &hqyj_24071[7].age,  &hqyj_24071[7].sco);
            
    fprintf(stdout , "名字:%s\t性别:%s\t学号:%d\t年龄:%d\t成绩:%d\t\n" , 
            hqyj_24071[7].name, hqyj_24071[7].sex, hqyj_24071[7].id,
            hqyj_24071[7].age,  hqyj_24071[7].sco);

    // ======================= 关闭文件 =======================
    file_close(&fp);
    return 0;
}

FILE *file_open(const char *path, const char *mode)
{
    FILE *fp = fopen(path, mode);
    if (NULL == fp)
    {
        printf("文件创建失败\n");
        exit(-1);
    }
    return fp;
}

int file_close(FILE **fp)
{
    fclose(*fp);
    *fp = NULL;
}